<?php
require_once ('BaseAjaxModule.php');

class PlaceModule extends BaseAjaxModule {
	
	public function getPlaceDetailTask()	{
		if (!$this->getRequest()->hasParam('id'))	{
			$this->view->result = 'Chưa nhập ID';
			return;
		}
		$id = $this->getParam('id');
		$place = new Model_Place();
		$place->id = $id;
		$result = $place->connect();
		if (!$result)	{
			$this->view->result = 'Địa điểm không tồn tại';
			return;
		}
		$user = $this->getUser();
		if ($user == null)
			$viewer_id = 0;
		else $viewer_id = $user->id;
		$this->view->result = $place->getInfo($viewer_id);
	}
	
	public function getNearPlacesTask(){
		if (!$this->getRequest()->hasParam('id'))	{
			$this->view->setAjaxResponse($this->createErrorResult("ID place is undefined."));
		}
		else{
			$place = new Model_Place();
			$place->setOption('id',$this->getParam('id'));
			if($place->connect()){
				$this->view->result = $place->getNearPlaces(0,5,0,50);
			}else{
				$this->view->setAjaxResponse($this->createErrorResult("place is undefined."));
			}
		}
	}
	public function voteTask(){
		$user = $this->getUser();
		if ($user == null){
			$this->view->setAjaxResponse($this->createErrorResult("Chưa đăng nhập!"));
		}
		else{
			if (!$this->getRequest()->hasParam('id'))	{
				$this->view->setAjaxResponse($this->createErrorResult("ID place is undefined."));
			}
			elseif(!$this->getRequest()->hasParam('value')){
				$this->view->setAjaxResponse($this->createErrorResult("Số bình chọn không hợp lệ."));
			}
			else{
				$place = new Model_Place();
				$place->setOption('id',$this->getParam('id'));
				if($place->connect()){
					$result = $place->vote($user->id,$this->getParam('value'));
					if(!$result) 
						$this->view->setAjaxResponse($this->createErrorResult("Bạn chỉ được bình chọn một lần!"));
					else $this->view->result = $result;
				}else{
					$this->view->setAjaxResponse($this->createErrorResult("place is undefined."));
				}
			}
		}
	}
}
?>